<?php
/**
 * 管理员管理
 * Created by gii.
 * User: Gii
 * Date: 2022/03/24
 * Time: 10:03
 */


namespace App\Http\Admin\controllers\System;

use App\Extend\Debug;
use App\Http\Admin\Logics\AccountLogic;
use App\Http\Admin\Logics\LoginLogic;
use App\Http\Admin\Logics\MenuLogic;
use App\Models\Models\System\SystemAccountAuth;
use Illuminate\Http\Request;

class AccountController extends BaseController
{
	public $title = '管理员';

	public function __construct()
	{
		parent::__construct();
	}

	// 表格页面
	public function index() {
        $search = [
            [
                'type'  => 'input',
                'key'   => 'id',
                'name'  => '请输入ID',
            ],
            [
                'type'  => 'input',
                'key'   => 'username',
                'name'  => '请输入用户名',
            ],
        ];
        $searchHtml = makeSearch($search, []);
		return $this->fetch(['search'=>$searchHtml]);
	}

	// 获取数据
    public function getData(Request $request) {
    	$page = $request->query('page', 1);
    	$pageSize = $request->query('limit', 10);

        $search = $request->query('key', []);

    	$data = AccountLogic::getListWithCount($search, '*', $page, $pageSize);
    	return $this->setSuccess($data->getList(), $data->getCount());
    }

	// 添加
	public function add(Request $request) {
		if($post = $request->postWithOutKeys()) {
			$res = AccountLogic::add($post);
			if($res) {
				return $this->setSuccess();
			} else {
				return $this->setError(Debug::getDebug());
			}
		}

		return $this->fetch(['get'=>[]], 'form');
	}

	// 编辑
	public function update(Request $request) {
		if($post = $request->postWithOutKeys()) {
			$res = AccountLogic::update($post);
			if($res === false) {
				return $this->setError(Debug::getDebug());
			} else {
				return $this->setSuccess();
			}
		}

		$id = $request->query('id');
		$get = AccountLogic::getOne($id);

		return $this->fetch(['get'=>$get], 'form');
	}

	// 删除
	public function del(Request $request) {
		if(!$id = $request->query('id')) {
			return $this->setError('请提交要删除的数据');
		}

		$res = AccountLogic::del($id);
		if(!$res) {
			return $this->setError('删除失败');
		}
		return $this->setSuccess();
	}

	// 状态
	public function status(Request $request) {
		if(!$id = $request->query('id')) {
			return $this->setError('请选择要修改的数据');
		}
		$status = $request->query('status');
		$res = AccountLogic::status($id, $status);
		if(!$res) {
			return $this->setError('操作失败');
		}
		return $this->setSuccess();
	}

    // 重置密码
    public function resetPassword(Request $request) {
        if(!$id = $request->query('id')) {
            return $this->setError('请选择要操作的数据');
        }
        $password = AccountLogic::resetPassword($id);
        if(!$password) {
            return $this->setError('密码重置失败');
        }
        return $this->setSuccess($password);
    }

    //编辑权限
    public function auth(Request $request) {
        if($post = $request->post()) {
            $res = SystemAccountAuth::changeMenuId($post['ids'], $post['id']);
            if($res === false) {
                return $this->setError(Debug::getDebug());
            }
            return $this->setSuccess();
        }
        
        $id = $request->query('id');
        if($id ==1) {
            throw new \Exception('超级管理员不需要管理权限');
        }

        $account = AccountLogic::getOne($id);

        return $this->fetch(['account'=>$account], 'auth');
    }

    public function getAuthTreeData(Request $request) {
        $accountId = $request->query('account_id');
        if(!$accountId) {
            throw new \Exception('请提交用户管理员');
        }
        return $this->setSuccess(MenuLogic::getTree($accountId));
    }

    // 修改密码
    public function changePassword(Request $request) {
        if($post = $request->post()) {
            $password = $post['password'] ?? '';
            $passwordNew = $post['password_new'] ?? '';
            $passwordAgain = $post['password_again'] ?? '';
            if(!$password || !$passwordNew || !$passwordAgain) {
                return $this->setError('参数缺失');
            }
            if($password != $passwordAgain) {
                return $this->setError('两次密码输入不一致');
            }
            
            $res = AccountLogic::changePassword($password, $passwordNew);
            if(!$res) {
                return $this->setError(Debug::getDebug());
            }
            LoginLogic::logout();
            return $this->setSuccess();
        }

        return $this->fetch();
    }
}